<?php
namespace Admin\Controller;
use Think\Controller;
class ProductController extends Controller {
	public function __construct(){
		parent::__construct();
		if(!$_SESSION['admin_is_login']){
			$this->redirect('admin/Auth/login');
		}
	}
    public function index(){
    	$product_model = M('product');
		$count=$product_model->count();
		$Page=new \Org\Util\Page($count,5);
		$show=$Page->show();
		//在使用join语句时，若两表的字段重名，则需要设别名如field("*,product.id as id,product_catalog.id as cid"),不然重名字段覆盖
		$products = $product_model->field("*,product.id as id")->join('LEFT JOIN product_catalog ON product_catalog.id = product.catalog_id')->order('product.id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('products',$products);
		$this->assign('page',$show);
		$this->display();
    }
    public function del($id){
    	M('product')->where('id='.$id)->delete();
    	$this->redirect('admin/Product/index');	
    }
    public function add(){
    	$level1=M('product_catalog')->where('parent_id=0')->select();
    	$this->assign('level1',$level1);
    	$this->display();
    }
    public function getCatalog(){
    	$id=I('get.id');
    	$data=M('product_catalog')->where('parent_id='.$id)->select();
    	$this->ajaxReturn($data);
    }
    public function do_add(){
    	$rules = array(
			    array('title','require','标题不能为空'),
			    array('catalog_id','require','分类不能为空'),
			    array('price','require','价格不能为空'),
			    array('stock','require','库存不能为空'),
			    array('desc','require','描述不能为空'),
			    array('content','require','内容不能为空'),
			    array('img','require','图片不能为空'),
			);
		$product_model = M("product");
		if (!$product_model->validate($rules)->create()){
		     $this->error($product_model->getError());
		}else{
			M('product')->add(array(
				'title'=>I('post.title'),
				'catalog_id'=>I('post.catalog_id'),
				'price'=>I('post.price'),
				'stock'=>I('post.stock'),
				'desc'=>I('post.desc'),
				'content'=>I('post.content'),
				'img'=>I('post.img')
			));
			$this->success('添加成功',U('admin/Product/index'));
		}
    }
    public function edit($id){
    	$product=M('product')->where('id='.$id)->find();
    	$level1_data=M('product_catalog')->where('parent_id=0')->select();
	    $level3=$product['catalog_id'];
	    $level3_catalog=M('product_catalog')->where('id='.$level3)->find();
	    $level2=$level3_catalog['parent_id'];
	    $level2_catalog=M('product_catalog')->where('id='.$level2)->find();
	    $level1=$level2_catalog['parent_id'];
	    $this->assign('level1',$level1);
	    $this->assign('level2',$level2);
	    $this->assign('level3',$level3);
    	$this->assign('level1_data',$level1_data);
    	$this->assign('product',$product);
    	$this->display();
    }
    public function do_edit(){
    	$rules = array(
			    array('title','require','标题不能为空'),
			    array('catalog_id','require','分类不能为空'),
			    array('brand_id','require','品牌不能为空'),
			    array('price','require','价格不能为空'),
			    array('stock','require','库存不能为空'),
			    array('desc','require','描述不能为空'),
			    array('content','require','内容不能为空'),
			    array('img','require','图片不能为空'),
			);
    	$id=I('post.id');
		$product_model = M("product");
		if (!$product_model->validate($rules)->create()){
		     $this->error($product_model->getError());
		}else{
			M('product')->where('id='.$id)->save(array(
				'title'=>I('post.title'),
				'catalog_id'=>I('post.catalog_id'),
				'brand_id'=>I('post.brand_id'),
				'price'=>I('post.price'),
				'stock'=>I('post.stock'),
				'desc'=>I('post.desc'),
				'content'=>I('post.content'),
				'img'=>I('post.img')
			));
			$this->success('编辑成功',U('admin/Product/index'));
		}
    }
    public function ban(){
    	$id=I('post.id');
    	M('product')->where('id='.$id)->save(array(
    		'status'=>1
    	));
    	echo 'ok';
    	//$this->redirect('admin/product/index');
    }
    public function enable(){
    	$id=I('post.id');
    	M('product')->where('id='.$id)->save(array(
    		'status'=>0
    	));
    	echo 'ok';
    	//$this->redirect('admin/product/index');
    }
}